<?php
/**
 * Created by PhpStorm.
 * User:wyy
 * Date: 2018/7/9
 * Time: 9:56
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
class Swiper extends Common
{
    public function swiperList(){
        //标题
        $this->templateTitle("幻灯片列表");

        $where = [];

        $pro = trim(input('pro'));
        $this->assign('area_province',$pro);
        if($pro){
            $where['pro'] = $pro;
        }

        $city = trim(input('city'));
        $this->assign('area_city',$city);
        if($city){
            $where['city'] = $city;
        }

        $cid = trim(input('cid'));
        $this->assign('cid',$cid);
        if($cid){
            $where['category_id'] = $cid;
        }

        //获取所有用户
        $list = Db::name("swiper")
            ->where($where)
            ->order("display_order desc,update_time DESC")
            ->paginate(10);

        // echo Db::name("admin_role")->getLastSql();
        $this->assign("list", $list);

        return $this->fetch();
    }
    //添加
    public function addSwiper(){
        if (request()->isGet())
        {
            $this->layoutModal();
            $this->templateTitle('添加幻灯片');

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "create_time" => now_datetime()
            );
            //排序
            $data["display_order"] = request()->param("ord");
            //分类图标
            $data["thumb"] = request()->param("thumb");
            if(empty($data['thumb'])){
                return $this->ajaxError('请上传图片');
            }
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }
            //是否显示
            $data["is_visible"] = request()->param("visible");
            $data["category_id"] = request()->param("cid");
            $data["link"] = request()->param("link");

            $result = Db::name('swiper')
                ->insert($data);
            if ($result)
            {
                return $this->ajaxSuccess("添加成功！");
            }
            else
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }
    //编辑
    public function editSwiper(){
        //ID
        $Id = request()->param("aid");

        if (request()->isGet())
        {
            $this->layoutModal();

            //获取信息
            $swiper= Db::name("swiper")
                ->where("id", $Id)
                ->find();
            $this->assign("swiper", $swiper);
            $this->assign("area_province", $swiper['pro']);
            $this->assign("area_city", $swiper['city']);
            $this->assign("single_image", array("thumb"=>$swiper["thumb"]));

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            //paixu
            $data["display_order"] = request()->param("ord");
            //图标
            $data["thumb"] = request()->param("thumb");
            if(empty($data['thumb'])){
                return $this->ajaxError('请上传图片');
            }
            $data['pro'] = request()->param("pro");
            if(empty($data['pro'])){
                return $this->ajaxError('请选择省份');
            }
            $data['city'] = request()->param("city");
            if(empty($data['city'])){
                return $this->ajaxError('请选择城市');
            }
            //是否显示
            $data["is_visible"] = request()->param("visible");
            $data["category_id"] = request()->param("cid");
            $data["link"] = request()->param("link");

            try
            {

                //更新分类ID路径
                Db::name("swiper")
                    ->where("id", $Id)
                    ->update($data);

                return $this->ajaxSuccess("更新成功！");
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }
    //删除
    public function removeSwiper(){
        if (request()->isAjax())
        {
            //获取ID
            $Id = request()->param("rid");
            // 删除
            $result = Db::name('swiper')
                ->where('id', $Id)
                ->delete();
            if($result){
                return $this->ajaxSuccess("删除成功！");
            }else{
                return $this->ajaxError("删除失败！");
            }

        }
    }
}